package com.zp.self.module.level_4_算法练习.数据结构.数组;
import org.junit.Test;

/**
 * @author By ZengPeng
 */
public class 力扣_999_可以被一步捕获的棋子数 {
    @Test
    public void main() {
        
        Solution solution = new 力扣_999_可以被一步捕获的棋子数().new Solution();
        System.out.println(solution.numRookCaptures(new char[][] {{'.','.','.','.','.','.','.','.'},{'.','.','.','p','.','.','.','.'},{'.','.','.','R','.','.','.','p'},{'.','.','.','.','.','.','.','.'},{'.','.','.','.','.','.','.','.'},{'.','.','.','p','.','.','.','.'},{'.','.','.','.','.','.','.','.'},{'.','.','.','.','.','.','.','.'}}));
    }

    /**
     题目：在一个 8 x 8 的棋盘上，有一个白色的车（Rook），用字符 'R' 表示。棋盘上还可能存在空方块，白色的象（Bishop）以及黑色的卒（pawn），分别用字符 '.'，'B' 和 'p' 表示。不难看出，大写字符表示的是白棋，小写字符表示的是黑棋。
         车按国际象棋中的规则移动。东，西，南，北四个基本方向任选其一，然后一直向选定的方向移动，直到满足下列四个条件之一：
         棋手选择主动停下来。
         棋子因到达棋盘的边缘而停下。
         棋子移动到某一方格来捕获位于该方格上敌方（黑色）的卒，停在该方格内。
         车不能进入/越过已经放有其他友方棋子（白色的象）的方格，停在友方棋子前。
         你现在可以控制车移动一次，请你统计有多少敌方的卒处于你的捕获范围内（即，可以被一步捕获的棋子数）。

         示例 1：
         输入：[[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".","R",".",".",".","p"],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."]]
         输出：3
         解释：
         在本例中，车能够捕获所有的卒。

         示例 2：
         输入：[[".",".",".",".",".",".",".","."],[".","p","p","p","p","p",".","."],[".","p","p","B","p","p",".","."],[".","p","B","R","B","p",".","."],[".","p","p","B","p","p",".","."],[".","p","p","p","p","p",".","."],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."]]
         输出：0
         解释：
         象阻止了车捕获任何卒。

         示例 3：
         输入：[[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".","p",".",".",".","."],["p","p",".","R",".","p","B","."],[".",".",".",".",".",".",".","."],[".",".",".","B",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".",".",".",".",".","."]]
         输出：3
         解释：
         车可以捕获位置 b5，d6 和 f5 的卒。

     分析：【P 💖】
        1.遍历：找到R,然后遍历行、列，碰到'B' 或 'p' 停下计算
                --执行耗时:0 ms,击败了100.00% 的Java用户
                --编码耗时:[23:23]
        大神.遍历：然后递归向一个方向查找;总共4个方向
                --代码更优雅

     边界值 & 注意点：
        1.
     **/
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
    int sum=0;
    public int numRookCaptures(char[][] board) {
        int[][] directions = new int[][]{{-1,0},{1,0},{0,-1},{0,1}};
        for (int row = 0; row < board.length; row++) {
            int clow = 0;
            for (; clow < board[0].length; clow++) {
                if(board[row][clow]=='R') {
                    for (int[] direction : directions) {
                        dfs(board,row+direction[0],clow+direction[1],direction[0],direction[1]);
                    }
                    break;
                }
            }
            if(clow < board[0].length)break;
        }
        return sum;
    }

    private void dfs(char[][] board, int x, int y, int increX, int increY) {
        if(x<0 || y<0 || x>7 || y>7)
            return;
        if(board[x][y] == 'B')
            return;
        if(board[x][y] == 'p'){
            sum++;
            return;
        }
        dfs(board,x+increX,y+increY,increX,increY);
    }

    public int numRookCaptures_Gc(char[][] board) {
        int row = 0,clow = 0,sum=0;
        for ( row = 0; row < board.length; row++) {
            for ( clow = 0; clow < board[0].length; clow++) {
               if(board[row][clow] =='R')
                    break;
            }
            if(clow<board[0].length )
                break;
        }
        int rowUp = row,clowl = clow;
        while (rowUp>=0){
            if(board[rowUp][clow] =='B')
                break;
            if(board[rowUp][clow] =='p'){
                sum++;
                break;
            }
            rowUp--;
        }
        while (clowl>=0){
            if(board[row][clowl] =='B')
                break;
            if(board[row][clowl] =='p'){
                sum++;
                break;
            }
            clowl--;
        }
        rowUp = row;
        clowl = clow;
        while (rowUp < board.length){
            if(board[rowUp][clow] =='B')
                break;
            if(board[rowUp][clow] =='p'){
                sum++;
                break;
            }
            rowUp++;
        }
        while (clowl < board[0].length){
            if(board[row][clowl] =='B')
                break;
            if(board[row][clowl] =='p'){
                sum++;
                break;
            }
            clowl++;
        }
        return sum;
    }
}
//leetcode submit region end(Prohibit modification and deletion)

}